package cn.wit.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class DemoLineChart extends Application{

	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		
		VBox box = new VBox();
		
		//线性代数
		Map<String, String> linearAlgebra = new HashMap<>();
		
		test(linearAlgebra,0,40);
		
		//高等数学
		Map<String, String> advancedAlgebra = new HashMap<>();
		
		test(advancedAlgebra,41,84);
		
		box.getChildren().add(createLineChart("线性代数",linearAlgebra));
		
		box.getChildren().add(createLineChart("高等代数 ",advancedAlgebra));
		
		Scene scene = new Scene(box);
		
		primaryStage.setScene(scene);
		
		primaryStage.show();
	}
	
	public static void main(String[] args) {
		launch(args);
	}
	
	//这里只是创建一个表格
	public LineChart<String, String> createLineChart(String title,Map<String,String> scores){
	
		//创建x轴和y轴
		Axis<String> xAxis = new CategoryAxis();
		
		Axis<String> yAxis = new CategoryAxis();
		
		yAxis.setLabel("分数");
		xAxis.setLabel("姓名");
		
		LineChart<String,String> lineChart = new LineChart<>(xAxis , yAxis);
		
		Series<String,String>  series = new Series<>();
		
		for(Entry<String, String> entry:scores.entrySet()) {
			series.getData().add(new Data<String,String>(entry.getKey(),entry.getValue()));
		}
		
		series.setName(title);
		
		lineChart.getData().add(series);
		
		return lineChart;
	}
	
	//可以定义一个方法创建多个表格
	
	public Map<String,String> test(Map<String,String>  Algebra,int m,int n) {
		
		String path = "D:\\暑期作业与笔记\\camp\\code\\day11\\data\\scores.csv";
		
		//定义两个map集合来存放，一个放高等代数，一个放线性代数
//		Map<String,String>  linearAlgebra = new HashMap<>();
//		Map<String,String>  advancedAlgebra = new HashMap<>();
        
		try(
				FileInputStream in = new FileInputStream(path);
				BufferedReader reader = new BufferedReader(new InputStreamReader(in,"gbk"));
				
		){
			
			reader.readLine();//第一行信息，为标题信息，不用,如果需要，注释掉 
			 
			String line = null;
			int count = 0;
			
			
			while((line=reader.readLine())!=null){  
                
				String item[] = line.split(",");//CSV格式文件为逗号分隔符文件，这里根据逗号切分 
                  
                String grade = item[item.length-5];//这就是你要的数据了
                
                String name = item[item.length-3];//这是学生姓名
                
                String course = item[item.length-4];//这是课程
                
                count++;
                
                if(count<=n&&count>=m) {
                	
                	Algebra.put(name, grade);
                	
                }
            }  
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return Algebra;
		
	}

}
